<#include "/java_copyright.include">
<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
package ${basepackage}.controller.admin;


import ${basepackage}.Message;
import ${basepackage}.Page;
import ${basepackage}.Pageable;
import ${basepackage}.management.entity.${className};
import ${basepackage}.management.service.${className}Manager;
import ${basepackage}.util.Constants;
import ${basepackage}.util.StringUtil;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

<#include "/java_imports.include">
@Controller("${classNameLower}Controller")
@RequestMapping("/admin/${classNameLower}")
public class ${className}Controller extends BaseController {

    private final static Logger LOG = LoggerFactory.getLogger(${className}Controller.class);

    private ${className}Service ${classNameLower}Service;

    @Resource (name = "${classNameLower}ManagerImpl")
    private ${className}Manager ${classNameLower}Manager;

    /**
     * 列表
     */
    @RequestMapping (value = "/list", method={RequestMethod.GET,RequestMethod.POST})
    public String list(Pageable pageable, ${className} ${classNameLower}, Model model){
        Map<String, Object> params = getValueMap(${classNameLower});
        params.put(Constants.IS_DELETE, false);
        Page<${className}> page = ${classNameLower}Manager.findPages(params, pageable);
        model.addAttribute ("${classNameLower}", ${classNameLower});
        model.addAttribute("page", page);
        return "/admin/${classNameLower}/${classNameLower}List";
    }

    /**
     * 跳转到表单页面,添加或者修改
     */
    @RequestMapping(value = "/toFormPage", method={RequestMethod.GET})
    public String toFormPage(String id, ModelMap model, RedirectAttributes redirectAttributes) {
        if (StringUtil.isEmpty(id)) {
            model.addAttribute(SupplierConstant.FORM_TYPE,SupplierConstant.FORM_TYPE_INSERT);
            model.addAttribute("${classNameLower}", new ${className}());
        } else {
            ${className} ${classNameLower} = ${classNameLower}Manager.findById(Long.valueOf(id));
            model.addAttribute("${classNameLower}", ${classNameLower});
            model.addAttribute(SupplierConstant.FORM_TYPE,SupplierConstant.FORM_TYPE_UPDATE);
        }
        return "/admin/${classNameLower}/${classNameLower}Form";
    }

    @RequestMapping(value = "/save", method={RequestMethod.POST})
    public String save(${className} ${classNameLower}, HttpServletRequest req, RedirectAttributes redirectAttributes) {
        try {
            ${classNameLower}.setIsDelete(false);
            ${classNameLower}Manager.save(${classNameLower});
            addFlashMessage (redirectAttributes, SUCCESS_MESSAGE_NEW);
        } catch (Exception e) {
            addFlashMessage (redirectAttributes, ERROR_MESSAGE_NEW);
            LOG.error(e.getMessage() ,e);
        }
        return "redirect:list.jhtml";
    }

    @RequestMapping(value = "/edit", method={RequestMethod.POST})
    public String edit(${className} ${classNameLower}, RedirectAttributes redirectAttributes) {
        try {
            ${classNameLower}Manager.update(${classNameLower});
            addFlashMessage (redirectAttributes, SUCCESS_MESSAGE_NEW);
        } catch (Exception e) {
            addFlashMessage (redirectAttributes, ERROR_MESSAGE_NEW);
            LOG.error(e.getMessage() ,e);
        }
        return "redirect:list.jhtml";
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method={RequestMethod.GET})
    public String delete(Long id, RedirectAttributes redirectAttributes) {
        try {
            ${classNameLower}Manager.delete(id);
            addFlashMessage (redirectAttributes, Message.success("deleteSuccess"));
            return "redirect:list.jhtml";
        } catch (Exception e) {
            LOG.error(e.getMessage() ,e);
            addFlashMessage (redirectAttributes, Message.error("deleteFailure"));
        return "redirect:list.jhtml";
        }
    }

    /**
     * 跳转到查看页面并携带查看数据
     * @param model
     * @return
     */
    @RequestMapping(value = "/toDetailPage", method={RequestMethod.GET})
    public String toDetailPage(Long id, ModelMap model) {
            model.addAttribute ("${classNameLower}", ${classNameLower}Manager.findById(id));
            return "/admin/${classNameLower}/${classNameLower}Detail";
        }
    }
